/**
 * Created by zengy on 2017/6/8.
 */
angular.module('mainApp.knowledge',['ui.router'])
    .config(function ($stateProvider) {
        $stateProvider
            .state({
                name:'knowledge/knowledgeList',
                url:'/knowledge/knowledgeList',
                templateUrl:'views/knowledge/knowledge_list.html?v='+new Date().getMinutes(),
                controller:'knowledgeCtrl'
            })
    })
    .controller('knowledgeCtrl',function ($scope) {
        $scope.$emit('arg',7)
        $scope.knowledgeModel={
            pageNo:1,   //查询页数
            pageSize:10 //每页数条数
        }
        $scope.addKnowledgeModel={};
        $scope.pagination = function (currentPage, pageCount) {
            $('.page-function #pagination').remove();
            $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
            $('.pagination').twbsPagination({
                totalPages: pageCount,
                startPage: currentPage,
                visiblePages: 5,
                first: "首页",
                prev: "上一页",
                next: "下一页",
                last: "尾页",
                href: 'javascript:;',
                //hideOnlyOnePage : true,
                onPageClick: function (event, page) {
                    $scope.informationList(page)
                }
            });
        };
        //知识列表
        $scope.informationList=function (page) {
            if(page){
                $scope.knowledgeModel.pageNo=page;
            }
            layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            new HttpRequest().informationList($scope.knowledgeModel, function (res) {
                layer.closeAll();
                if (res.code == '000') {

                    $scope.knowledgeList= res.data.list;
                    $scope.$apply();
                    if ($scope.knowledgeList.length > 0) {
                        $scope.pagination($scope.knowledgeModel.pageNo, res.data.pageCount);
                    }
                } else {
                    layer.msg('获取列表失败！')
                }
            })
        }
        $scope.informationList($scope.knowledgeModel.pageNo)
        //删除
        $scope.del=function (event) {
            var _this=$(event.target);
            var rowId=_this.attr('data-rowId');
            var index = layer.confirm('是否要删除本条内容？', {
                title: '删除确认',
                btn: ['确定', '取消'],//按钮
            }, function () {
                new HttpRequest().deleteWxInformation({wxInformationId:rowId},function (res) {
                    if(res.code == '000'){
                        location.reload()
                    }else{
                        layer.msg('操作失败！');
                    }
                })
            }, function () {
                layer.close(index)
            })
        }
        //启用
        $scope.enable=function (event) {
            var _this=$(event.target);
            var rowId=_this.attr('data-rowId');
            new HttpRequest().enableWxInformation({wxInformationId:rowId},function (res) {
                if(res.code == '000'){
                    location.reload();
                }else{
                    layer.msg('操作失败！');
                }
            })
        }
        //禁用
        $scope.disEnable=function (event) {
            var _this=$(event.target);
            var rowId=_this.attr('data-rowId');

            new HttpRequest().disableInformation({wxInformationId:rowId},function (res) {
                if(res.code == '000'){
                    location.reload();
                }else{
                    layer.msg('操作失败！');
                }
            })
        }
        //编辑增加
        $scope.showDetail=function (event) {
            var _this=$(event.target);
            var datas=_this.data('detail');

            if(!datas){
                $scope.addKnowledgeModel=null;
                $('select[name="cate"] option').eq(0).prop('selected',true)
            }else{
                $('select[name="cate"] option[data-text="'+datas.category+'"]').prop('selected',true)
                $scope.addKnowledgeModel=datas;
                $scope.addKnowledgeModel.rowId=datas.wxInformationId;
                delete $scope.addKnowledgeModel.wxInformationId;
            }
            $scope.basePictureUrl=basePictureUrl;

        }
        //编辑保存
        $scope.commit=function () {
            delete $scope.addKnowledgeModel.category;
            delete $scope.addKnowledgeModel.publishTime;
            delete $scope.addKnowledgeModel.status;
            $scope.addKnowledgeModel.smDictEnumId=$('select[name="cate"]').val();
            new HttpRequest().saveInformation($scope.addKnowledgeModel,function (res) {
                if(res.code == '000'){
                    location.reload()
                }else{
                    layer.msg('操作失败！')
                }
            })
        }
        //置顶
        $scope.setTop=function (event) {
            var _this=$(event.target);
            var rowId=_this.attr('data-rowId');
            new HttpRequest().putInformationTop({wxInformationId:rowId},function (res) {
                if(res.code == '000'){
                    location.reload();
                }else{
                    layer.msg('操作失败！');
                }
            })
        }
        //上传封面
        $scope.uploadImg = function (current, type) {
            if(current.files.length>0){
                $('.imgCover').attr('src','./img/img-cover.png');
                new HttpRequest().uploadFile(current,type,function (res) {
                    if(res.code == '000'){
                        var resData = JSON.parse(res.data).url;
                        console.log(resData);
                        $scope.addKnowledgeModel.coverImgUrl = resData;
                        $('.imgCover').attr('src',basePictureUrl+resData);
                    }else{
                        layer.msg('上传封面失败'+'('+res.msg+')');
                    }
                })
            }
        };
        //获取分类
        $scope.getCategoryMsg=function () {
            new HttpRequest().getCategoryMsg(function (res) {
                if(res.code=='000'){
                    $scope.category=res.data;
                }
            })
        }
        $scope.getCategoryMsg();



    })
